package com.feige.config;


import com.feige.cache.CacheTemplate;
import com.feige.filter.JwtFilter;
import com.feige.handler.TokenLogoutHandler;
import com.feige.handler.UnauthorizedEntryPoint;
import com.feige.jwt.JwtTokenHelper;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;



/**
 * springSecurity 配置类
 * @author feige
 */
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@AllArgsConstructor
public class TokenWebSecurityConfig {

    private final UserDetailsService userDetailsService;

    private final CacheTemplate<String, Object> cacheTemplate;

    private final JwtTokenHelper jwtTokenHelper;

    
    
    
    /**
     * 配置设置
     * @param http
     * @throws Exception
     */
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http.exceptionHandling()
                .authenticationEntryPoint(new UnauthorizedEntryPoint())
                //禁用csrf
                .and().cors()
                .and().csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                //允许基于使用HttpServletRequest限制访问
                .and().authorizeRequests().anyRequest().authenticated()
                //指定登出URL
                .and().logout().logoutUrl("/api/logout").permitAll().logoutSuccessHandler(new TokenLogoutHandler(cacheTemplate,jwtTokenHelper))
                .and().addFilterBefore(new JwtFilter(cacheTemplate,jwtTokenHelper), UsernamePasswordAuthenticationFilter.class).httpBasic()
                .and().build();
    }
    
    
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }


    @Bean
    public AuthenticationManager authenticationManager(HttpSecurity http, PasswordEncoder passwordEncoder) throws Exception {
        return http.getSharedObject(AuthenticationManagerBuilder.class)
                .userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder)
                .and()
                .build();
                
    }

   
}
